﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class Balance:CharDrawAction
    {
        private const int maxHaro = 5,
                          harroHealth = 100000,
                          phaseDuration = 20000;
        private int healthTrigger = 90;
        private bool phaseActive = false;
        private Timer phaseTimer = Core.CreateTimer();
        private List<Haro> haroList = new List<Haro>();

        public Balance(Character c, bool persistant, int row, int colEnd, int count, int lockoutLevel)
            :base(c, persistant, row, colEnd, count, lockoutLevel)
        {
            IsActive = true;
        }

        public override void DoAction(GameController gc)
        {
            if (haroList.Count == 0)
                CreateHaro(gc);
            if (Core.TimerTicks(phaseTimer) > phaseDuration && phaseActive)
            {
                Reset(gc);
                foreach (Haro h in haroList)
                {
                    if (((float)h.health / (float)h.maxHealth) * 100f < ((float)c.Health / (float)c.maxHealth) * 100f - 5
                        || ((float)h.health / (float)h.maxHealth) * 100f > ((float)c.Health / (float)c.maxHealth) * 100f + 5)
                        h.BuffBoss();
                    h.Invunerable = true;
                    h.health = h.maxHealth;
                }

                if (healthTrigger >= ((float)c.Health / (float)c.maxHealth) * 100f)
                    healthTrigger -= 10;
            }

            if (((float)c.health / (float)c.maxHealth) * 100f >= healthTrigger) return;

            if (phaseActive)
            {
                if (!c.GlobalLockout)
                    c.GlobalLockout = true;
                foreach (Haro h in haroList)
                    h.DoAction(gc);
            }

            if (!phaseActive && Core.TimerTicks(phaseTimer) == 0 && ((float)c.health / (float)c.maxHealth) * 100f <= healthTrigger)
            {
                phaseActive = true;
                c.Invunerable = true;
                foreach (Ability a in c.CurrentSet.SetOfAbilities)
                {
                    if (a.AbilityAction != this && a.AbilityAction.IsActive)
                        a.AbilityAction.Reset(gc);
                }
                Core.StartTimer(phaseTimer);
                foreach (Haro h in haroList)
                    h.Invunerable = false;
                GlobalConst.Utils.ActivateAnimationRow(c.ObjectSprite, c.offSetSpriteRow(), GlobalConst.DefaultColEnd, GlobalConst.DefaultFPC);
            }

        }

        public override void Draw()
        {
            if (phaseActive)
                foreach (Haro h in haroList)
                    h.DrawLines();
        }

        private void CreateHaro(GameController gc)
        {
            string[] haroName = new string[5];
            int haroStartPosX = 150,
                haroStartPosY = 720;
            haroName[0] = "Haro, the Balance of Rage";
            haroName[1] = "Haro, the Balance of Defense";
            haroName[2] = "Haro, the Balance of Speed";
            haroName[3] = "Haro, the Balance of Health";
            haroName[4] = "Haro, the Balance of Avoidance";
            
            Sprite haroSprite;
            Haro tempHaro;
            for (int i = 0; i < maxHaro; i++)
            {
                haroSprite = Sprites.CreateSprite(Resources.LoadBitmap(Resources.PathToResource("CharSprites\\haro.png")));
                haroSprite.X = haroStartPosX;
                haroSprite.Y = haroStartPosY;
                tempHaro = new Haro(c, haroName[i], harroHealth, haroSprite);
                gc.GameStages.Peek().ToAdd.Add(tempHaro);
                haroList.Add(tempHaro);
                haroStartPosX += 225;
            }
        }

        public override void Reset(GameController gc)
        {
            c.Invunerable = false;
            Core.StopTimer(phaseTimer);
            phaseActive = false;
            c.GlobalLockout = false;
        }

    }
}
